package edu.msg.jthr.web.servlets;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.model.Role;
import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.service.RoleService;
import edu.msg.jthr.backend.service.UserGroupService;
import edu.msg.jthr.backend.service.UserService;
import edu.msg.jthr.web.beans.RoleListBean;
import edu.msg.jthr.web.util.RoleAssembler;

/**
 * Servlet implementation class AddUser
 */
@WebServlet("/AddUser")
public class AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static Logger LOG = LoggerFactory.getLogger(AddUser.class);
	
	@EJB
	private UserService userService;
	@EJB
	private RoleService roleService;
	@EJB
	private UserGroupService userGroupService;
	
	private HttpSession session;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddUser() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		session = request.getSession(true);
		User user = (User) session.getAttribute("activeUser");
		if (user == null) {
			user = userService.getUserByUserName(request.getUserPrincipal()
					.toString());
			String role = RoleAssembler.getRole(user.getUserGroups());
			session.setAttribute("role", role);
			session.setAttribute("activeUser", user);
		}
		List<Role> roleList = Collections.emptyList();
		RoleListBean roleListBean = new RoleListBean(roleList);
		
		try {
			roleList = roleService.getAll();
			roleListBean.setRoleList(roleList);
		} catch (Exception ex) {
			LOG.error("Failure by roleBeans setting!" , ex);
		}
		
		session.setAttribute("roleListBean", roleListBean);
		
		request.getRequestDispatcher("/pages/AddUser.jsp").forward(request,
				response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		/*
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String telnumber = request.getParameter("telhome");
		String telmobilenumber = request.getParameter("telmob");
		String email = request.getParameter("email");
		String function = request.getParameter("function");
		String description = request.getParameter("description");
		String password = request.getParameter("password");
		String[] roles = request.getParameterValues("roles");
		
		User user = new User();
		user.setFirstName(firstname);
		user.setLastName(lastname);
		user.setTelNumber(telnumber);
		user.setTelMobileNumber(telmobilenumber);
		user.setMail(email);
		user.setFunctionAtComp(function);
		user.setDescription(description);
		user.setPassword(password);
		user.setUserName(createUsername(lastname, firstname));
		*/
		
		//List<Role> roles = roleService.getAll();
		
		/*
		List<Role> rolesList = new ArrayList<Role>();
		for (Role r : roles) {
			r = new Role();
			r.setRoleName(r.getRoleName());
			rolesList.add(r);
			roleService.update(r);
		}
		*/
		
		/*
		
		rolesList = roleService.getAll();
		
		List<UserGroup> userGroupList = new ArrayList<UserGroup>();
		
		UserGroup ug = new UserGroup();
		for (int i = 0; i < roles.length; i++) {
			
			boolean add = true;
			
			for (Role r : rolesList ) {
				if (roles[i] == r.getRoleName()) {
					add = false;
					ug.setRole(r);
				}
			}

			if (add) {
				Role r = new Role();
				r.setRoleName(roles[i]);
				roleService.create(r);
				ug.setRole(r);
			}
			
			userGroupList.add(ug);
		}
		
		ug.setUser(user);
		userGroupService.create(ug);
		
		user.setUserGroups(userGroupList);
		
		*/
		
		//userService.create(user);
		
		
		String add = "/pages/AddUser.jsp";
		RequestDispatcher dispatcher = request.getRequestDispatcher(add);
		dispatcher.forward(request, response);

	}

	@SuppressWarnings("unused")
	private String createUsername(String lastname, String firstname) {

		String un = lastname;
		un += Character.toString(firstname.charAt(0));

		return un;
	}

}
